#include <bits/stdc++.h>
// 2025/07/09
// tag: graph, functional graph, cycle detection
// Author: Forever 
using namespace std;
using int64 = int64_t;

int parents[53], parents2[54], to[53], in[53], cnt2, cnt3;
string a, b;
#define to_int(x) (x >= 'a' ? x - 'a' + 27 : x - 'A' + 1)

void init()
{
    for (int i = 1; i <= 52; i++)
        parents[i] = i, parents2[i] = i;
    parents2[53] = 53;
};

int find(int i)
{
    if (parents[i] != i)
        return parents[i] = find(parents[i]);
    return parents[i];
}

int find2(int i)
{
    if (parents2[i] != i)
        return parents2[i] = find2(parents2[i]);
    return parents2[i];
}

void un(int i, int j)
{
    int ri = find(i), rj = find(j);
    if (ri == rj)
        return;
    parents[ri] = rj;
}

void un2(int i, int j)
{
    int ri = find2(i), rj = find2(j);
    if (ri == rj)
        return;
    parents2[ri] = rj;
}

void solve()
{
    memset(to, 0, sizeof(to));
    memset(in, 0, sizeof(in));
    cin >> a >> b;
    init();
    cnt2 = cnt3 = 0;

    for (int i = 0; i < a.size(); i++) {
        int x = to_int(a[i]), y = to_int(b[i]);
        if (to[x] == 0)
            to[x] = y;
        else if (to[x] != y) {
            cout << -1 << endl;
            return;
        }
        un(x, y), un2(x, y);
    }

    for (int i = 1; i <= 52; i++)
        if (to[i] != 0) 
            in[to[i]]++;

    for (int i = 1; i <= 52; i++)
        if (in[i])
            cnt2++;

    if (cnt2 == 52 && a != b) {
        cout << -1 << endl;
        return;
    }

    int base_operations = 0;
    for (int i = 1; i <= 52; i++) 
        if (to[i] != 0 && to[i] != i) 
            base_operations++;

    for (int i = 1; i <= 52; i++) 
        if (!to[i] || to[i] == i || in[i] != 1)
            un2(i, 53);
    for (int i = 1; i <= 52; i++)
        if (find2(i) != 53)
            cnt3++, un2(i, 53);

    cout << base_operations + cnt3 << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}